{extend name="index/iframe" /}

{block name="content"}
<form class="layui-form layui-form-my" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">选择渠道</label>
            <div class="layui-input-inline">
                <select name="cps_id" id="cps_id" lay-filter="cps_id" lay-search="">
                    <option value="">选择渠道（全部）</option>
                    {volist name="$channel_data" id="vo"}
                    <option value="{$vo.cps_id}">{$vo.cps_id}：{$vo.cps_name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">对账日期</label>
            <div class="layui-input-inline" id="audit_time">
                <input type="text" name="date" class="layui-input" lay-verify="required" id="date" placeholder="yyyy-MM-dd - yyyy-MM-dd"  lay-verify="date" autocomplete="off">
            </div>
        </div>
    </div>
    <hr>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">CPS充值</label>
            <div class="layui-input-inline">
                <input type="text" value="0.00" name="fact_money" id="fact_money" placeholder="元" autocomplete="off" class="layui-input layui-disabled" disabled>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">分成比例</label>
            <div class="layui-input-inline">
                <input type="text" value="0" name="pay_rate" id="pay_rate" lay-verify="required" placeholder="%" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">通道费率</label>
            <div class="layui-input-inline">
                <input type="text" value="0" name="channel_rate" id="channel_rate" lay-verify="required" placeholder="%" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">税率</label>
            <div class="layui-input-inline">
                <input type="text" value="0" name="tax_rate" id="tax_rate" lay-verify="required" placeholder="%" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">参考金额</label>
            <div class="layui-input-inline">
                <input type="text" value="0.00" name="refer_money" id="refer_money" lay-verify="required" placeholder="元" autocomplete="off" class="layui-input layui-disabled" disabled>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">分成金额</label>
            <div class="layui-input-inline">
                <input type="text" value="0.00" name="end_money" id="end_money" lay-verify="required" placeholder="元" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <hr>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">收款人</label>
            <div class="layui-input-inline">
                <input type="text" value="" name="contact_man" id="contact_man" placeholder="收款人姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">银行账号</label>
            <div class="layui-input-inline">
                <input type="text" value="" name="open_account" id="open_account" placeholder="银行账号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">所属银行</label>
            <div class="layui-input-inline">
                <input type="text" value="" name="open_name" id="open_name" placeholder="所属银行" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">开户银行</label>
            <div class="layui-input-inline">
                <input type="text" value="" name="open_bank" id="open_bank" placeholder="开户银行" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class=layui-inline">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="description" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn my-btn" lay-submit="" lay-filter="submitBtn" >立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
{/block}

{block name="css"}{/block}

{block name="js"}
<script>
    layui.use(['form', 'layedit', 'laydate', 'upload', 'element'], function(){
        var form = layui.form
            ,$ = layui.$
            ,layer = layui.layer
            ,laydate = layui.laydate;

        //选择游戏
        form.on('select(cps_id)', function(data){
            var cps_id = data.value
                ,date = $('input[name="date"]').val()

            if(cps_id){
                time(cps_id);
            }

            if(cps_id && date){
                select(cps_id, date);
            }
        });

        //获取对账时间
        function time(cps_id){
            var load = layer.load();
            form.render();

            $.post('{:url("AuditCpsMng/time")}',{cps_id: cps_id}, function(json){
                layer.close(load);
                if(json.code==0){
                    var  d = new Date().getTime()
                        , html = '<input type="text" name="date" class="layui-input" lay-verify="required" id="date'+d+'" placeholder="yyyy-MM-dd - yyyy-MM-dd"  lay-verify="date" autocomplete="off">';
                    $('#audit_time').html(html);
                    console.dir(json.data);
                    laydate.render({
                        elem: '#date'+d
                        ,type: 'date'
                        ,range: true
                        ,min: json.data
                        ,max: '0'
                        ,done: function(value, date, endDate){
                            var cps_id = $('#cps_id').val()
                                ,date = value;
                            if(cps_id && date){
                                select(cps_id, date);
                            }
                        }
                    });
                }else{
                    layer.msg(json.msg, {icon: 0, time: 1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        }

        //查询数据
        function select(cps_id, date){
            var load = layer.load();
            $.post('{:url("AuditCpsMng/select")}',{cps_id: cps_id, date: date}, function(json){
                layer.close(load);
                if(json.code==0){
                    $('#fact_money').val(json.data.fact_money);
                    $('#pay_rate').val(json.data.pay_rate);
                    $('#channel_rate').val(json.data.channel_rate);
                    $('#tax_rate').val(json.data.tax_rate);
                    $('#refer_money').val(json.data.end_money);
                    $('#end_money').val(json.data.end_money);
                    $('#contact_man').val(json.data.contact_man);
                    $('#open_account').val(json.data.open_account);
                    $('#open_name').val(json.data.open_name);
                    $('#open_bank').val(json.data.open_bank);
                }else{
                    layer.msg(json.msg, {icon: 0, time: 1000});
                    $('input[name="date"]').val('');
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        }

        $('#channel_rate').blur(function(){
            var fact_money = $('#fact_money').val()
                ,pay_rate = $('#pay_rate').val()
                ,channel_rate = $('#channel_rate').val()
                ,tax_rate = $('#tax_rate').val()
                ,refer_money = (fact_money*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2);

            $('#refer_money').val(refer_money);
            $('#end_money').val(refer_money);
        })
        $('#tax_rate').blur(function(){
            var fact_money = $('#fact_money').val()
                ,pay_rate = $('#pay_rate').val()
                ,channel_rate = $('#channel_rate').val()
                ,tax_rate = $('#tax_rate').val()
                ,refer_money = (fact_money*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2);

            $('#refer_money').val(refer_money);
            $('#end_money').val(refer_money);
        })
        $('#pay_rate').blur(function(){
            var fact_money = $('#fact_money').val()
                ,pay_rate = $('#pay_rate').val()
                ,channel_rate = $('#channel_rate').val()
                ,tax_rate = $('#tax_rate').val()
                ,refer_money = (fact_money*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2);

            $('#refer_money').val(refer_money);
            $('#end_money').val(refer_money);
        })

        form.on('submit(submitBtn)', function(data){
            var load = layer.load();
            $.post('{:url("AuditCpsMng/add")}',data.field, function(json){
                layer.close(load);
                json.code==0 ? layer.msg(json.msg, {icon: 1, time: 1000}) : layer.msg(json.msg, {icon: 0, time: 1000});
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
            return false;
        });
    });
</script>
{/block}